<!DOCTYPE html><html lang="[&quot;zh-Hans&quot;,&quot;zh-CN&quot;,&quot;default&quot;]"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="开发并发布和更新一个npm包"><meta name="keywords" content="react,npm"><meta name="author" content="wenmu"><meta name="copyright" content="wenmu"><title>开发并发布和更新一个npm包 | 温木的博客</title><link rel="shortcut icon" href="/melody-favicon.ico"><link rel="stylesheet" href="/css/index.css?version=1.6.1"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.6.1"><link rel="dns-prefetch" href="https://cdn.staticfile.org"><link rel="dns-prefetch" href="https://cdn.bootcss.com"><link rel="dns-prefetch" href="https://creativecommons.org"><link rel="dns-prefetch" href="http://ta.qq.com"><script>(function() {
   var hm = document.createElement("script");
   hm.src = "https://tajs.qq.com/stats?sId=&lt;script type=&quot;text/javascript&quot; src=&quot;http://tajs.qq.com/stats?sId=66523541&quot; charset=&quot;UTF-8&quot;&gt;&lt;/script&gt;";
   var s = document.getElementsByTagName("script")[0];
   s.parentNode.insertBefore(hm, s);
 })();</script><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  }
} </script><meta name="generator" content="Hexo 4.2.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#准备工作"><span class="toc-number">1.</span> <span class="toc-text">准备工作</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#查看和设置-npm-源"><span class="toc-number">1.1.</span> <span class="toc-text">查看和设置 npm 源</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#搭建开发环境"><span class="toc-number">2.</span> <span class="toc-text">搭建开发环境</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#package-json"><span class="toc-number">2.1.</span> <span class="toc-text">package.json</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#webpack-config-js"><span class="toc-number">2.2.</span> <span class="toc-text">webpack.config.js</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#babel-的配置"><span class="toc-number">2.3.</span> <span class="toc-text">babel 的配置</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#打包发布"><span class="toc-number">3.</span> <span class="toc-text">打包发布</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#本地调试-link"><span class="toc-number">4.</span> <span class="toc-text">本地调试 link</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#link组件到global-node-modules"><span class="toc-number">4.1.</span> <span class="toc-text">link组件到global node_modules</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用link的组件"><span class="toc-number">4.2.</span> <span class="toc-text">使用link的组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#解除link"><span class="toc-number">4.3.</span> <span class="toc-text">解除link</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#更新-npm-包"><span class="toc-number">5.</span> <span class="toc-text">更新 npm 包</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#You-must-sign-up-for-private-packages"><span class="toc-number">6.</span> <span class="toc-text">You must sign up for private packages</span></a></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://i.loli.net/2020/02/27/xwaBoOj158MfNyq.jpg"></div><div class="author-info__name text-center">wenmu</div><div class="author-info__description text-center">记录一些在工作中或学习新知识时学到的一些知识点；正所谓好记性不如一个烂笔头，温故而知新！</div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">115</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">52</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">分类</span><span class="pull-right">30</span></a></div></div></div><div id="content-outer"><div class="no-bg" id="top-container"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">温木的博客</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus"><a class="site-page" href="/">首页</a><a class="site-page" href="/archives">文章</a><a class="site-page" href="/tags">标签</a><a class="site-page" href="/categories">分类</a></span></div><div id="post-info"><div id="post-title">开发并发布和更新一个npm包</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-01-08</time><span class="post-meta__separator">|</span><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/categories/npm/">npm</a></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>最近开发了一个 npm 包，并发布到了官网上，下面记录下开发过程。<br><a href="https://github.com/codepandy/tabheader_react" target="_blank" rel="noopener">github 代码地址</a></p>
<a id="more"></a>

<h2 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h2><ol>
<li>注册 npm 账号</li>
<li>认证 npm，不然不能发布包；（会给你发认证邮件）</li>
<li>设置 npm 源为官网</li>
</ol>
<h3 id="查看和设置-npm-源"><a href="#查看和设置-npm-源" class="headerlink" title="查看和设置 npm 源"></a>查看和设置 npm 源</h3><p>在国内我们通常都设置成了淘宝的仓库（<a href="https://registry.npm.taobao.org），这样是发布不了的。需要设置回官网的仓库连接（https://registry.npmjs.org/）。" target="_blank" rel="noopener">https://registry.npm.taobao.org），这样是发布不了的。需要设置回官网的仓库连接（https://registry.npmjs.org/）。</a></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">// 查看</span><br><span class="line">npm config get registry</span><br><span class="line">// 设置</span><br><span class="line">npm config <span class="built_in">set</span> registry https://registry.npmjs.org/</span><br></pre></td></tr></table></figure>

<h2 id="搭建开发环境"><a href="#搭建开发环境" class="headerlink" title="搭建开发环境"></a>搭建开发环境</h2><p>由于我使用的 react，搭建环境就是搭建 react 的开发环境。这里不做过多介绍。<br>只介绍几个发布组件相关的配置和注意事项。</p>
<h3 id="package-json"><a href="#package-json" class="headerlink" title="package.json"></a>package.json</h3><ul>
<li>main 字段设置的就是在引入你的程序时，执行的文件。</li>
<li>files 设置的是要发布到 npm 的文件。如果省略掉这一项，所有文件包括源代码会被一起上传到 npm</li>
<li>keywords 设置搜索关键字</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">"name"</span>: <span class="string">"tabheader-react"</span>,</span><br><span class="line"><span class="string">"version"</span>: <span class="string">"1.0.2"</span>,</span><br><span class="line"><span class="string">"description"</span>: <span class="string">"a tabheader component for react."</span>,</span><br><span class="line"><span class="string">"main"</span>: <span class="string">"./build/bundle.js"</span>,</span><br><span class="line"><span class="string">"files"</span>: [</span><br><span class="line">  <span class="string">"build"</span></span><br><span class="line">],</span><br><span class="line"><span class="string">"scripts"</span>: &#123;</span><br><span class="line">  <span class="string">"start"</span>: <span class="string">"webpack-dev-server --config webpack.dev.js"</span>,</span><br><span class="line">  <span class="string">"build"</span>: <span class="string">"webpack --config webpack.pro.js"</span></span><br><span class="line">&#125;,</span><br><span class="line"><span class="string">"keywords"</span>: [</span><br><span class="line">  <span class="string">"react"</span>,</span><br><span class="line">  <span class="string">"tab"</span>,5</span><br><span class="line">  <span class="string">"header"</span>,</span><br><span class="line">  <span class="string">"tabheader"</span>,</span><br><span class="line">  <span class="string">"tabheader-react"</span></span><br><span class="line">],</span><br><span class="line"><span class="string">"author"</span>: <span class="string">"wenmu"</span>,</span><br></pre></td></tr></table></figure>

<p><a href="https://www.npmjs.cn/files/package.json/" target="_blank" rel="noopener">package.json 详情介绍</a></p>
<h3 id="webpack-config-js"><a href="#webpack-config-js" class="headerlink" title="webpack.config.js"></a>webpack.config.js</h3><p>webpack 的配置需要注意的就是生产环境时，设置<code>libraryTarget</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">output: &#123;</span><br><span class="line">  path: path.resolve(__dirname, &quot;build&quot;),</span><br><span class="line">  filename: &quot;bundle.js&quot;,</span><br><span class="line">  libraryTarget: &quot;commonjs2&quot;,</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<p>libraryTarget 设置暴露的包类型，默认是 var，也就是变量的方式。这个要设置成模块的方式，这样可以通过模块的方式来引入。</p>
<h3 id="babel-的配置"><a href="#babel-的配置" class="headerlink" title="babel 的配置"></a>babel 的配置</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  &quot;presets&quot;: [&quot;@babel&#x2F;preset-env&quot;, &quot;@babel&#x2F;preset-react&quot;],</span><br><span class="line">  &quot;plugins&quot;: [&quot;@babel&#x2F;plugin-proposal-class-properties&quot;]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="打包发布"><a href="#打包发布" class="headerlink" title="打包发布"></a>打包发布</h2><p>程序开发完后，就可以打包发布了。上面也配置了发布的文件是哪些。<br>打包和发布其实也很简单，就是单纯的把组件的代码打包然后发布就行了。<br>在组件代码的根目录执行下面的命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm build</span><br></pre></td></tr></table></figure>

<p>打包成功后，登录 npm 然后发布组件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">npm login</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 登录成功后，执行发布命令</span><br><span class="line">npm publish</span><br></pre></td></tr></table></figure>

<p>成功后，就可以在<a href="https://www.npmjs.com/settings/wenmu/packages" target="_blank" rel="noopener">npm 官网</a>查看你发布的 package，并且在<code>npm</code>也能搜索到了。</p>
<h2 id="本地调试-link"><a href="#本地调试-link" class="headerlink" title="本地调试 link"></a>本地调试 link</h2><p>在开发组件时，还没发布到仓库，如何安装调试呢？这个可以使用<code>npm link</code>命令来实现。</p>
<h3 id="link组件到global-node-modules"><a href="#link组件到global-node-modules" class="headerlink" title="link组件到global node_modules"></a><code>link</code>组件到<code>global node_modules</code></h3><p>在组件的根目录执行如下命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm link</span><br></pre></td></tr></table></figure>

<blockquote>
<p>如果组件文件是在根目录，那么 link 后，代码有改动，引用的项目里面立马会体现出来，但是如果代码是在 src 目录下，需要 build 代码项目中才能看到修改后的效果；也可以直接是 src 目录中 link</p>
</blockquote>
<p>使用下面命令查看是否 link 成功</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">// 查看全局安装的包</span><br><span class="line">npm list -g --depth 0</span><br></pre></td></tr></table></figure>

<h3 id="使用link的组件"><a href="#使用link的组件" class="headerlink" title="使用link的组件"></a>使用<code>link</code>的组件</h3><p>进入到项目的根目录，执行命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm link your_component_name</span><br></pre></td></tr></table></figure>

<p>成功后，就可以在你的项目中使用组件了，使用方式和安装的组件一样。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">import TabHeader from <span class="string">'tabheader_react'</span></span><br></pre></td></tr></table></figure>

<h3 id="解除link"><a href="#解除link" class="headerlink" title="解除link"></a>解除<code>link</code></h3><ol>
<li>在项目中解除<br>进入引用组件的根目录，执行命令</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm unlink your_component_name</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>从<code>global node_modules</code>解除<br>进入组件的根目录，执行下面命令，注意后面不要跟组件名</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm unlink</span><br></pre></td></tr></table></figure>

<h2 id="更新-npm-包"><a href="#更新-npm-包" class="headerlink" title="更新 npm 包"></a>更新 npm 包</h2><p>发布了 npm 包后，如果有修改，我们需要发布更新，步骤如下：</p>
<ol>
<li>提交代码，如果你的代码已经用<code>git</code>托管，先提交代码，不然发布会报错</li>
<li>修改版本号<br>修改版本号，官方提供了的命令，当执行后，package.json 文件会自动被修改。命令如下：<br>比如现在版本是 1.0.0</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">// 最后一位加1，执行命令后未：1.0.1</span><br><span class="line">npm version patch</span><br><span class="line"></span><br><span class="line">// 第二位加1，执行后：1.1.0</span><br><span class="line">npm version minor</span><br><span class="line"></span><br><span class="line">// 第一位加1，执行后：2.0.0</span><br><span class="line">npm version major</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>发布更新<br>通过上面修改过版本号后，执行下面命令重新发布，线上的就会被更新了。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm publish</span><br></pre></td></tr></table></figure>

<h2 id="You-must-sign-up-for-private-packages"><a href="#You-must-sign-up-for-private-packages" class="headerlink" title="You must sign up for private packages"></a>You must sign up for private packages</h2><p>今天在发布包时，提示类似的名字已经有了，建议加上范围，如(“@wenmu/steps-react”)，但是加上后就报错了。一直提示<code>You must sign up for private packages</code>.</p>
<p>Scoped packages 会被自动发布为私有包，发布成公共的就可以了。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm publish --access=public</span><br></pre></td></tr></table></figure>

<p><a href="https://www.jianshu.com/p/db6113c94dbc" target="_blank" rel="noopener">参考链接</a></p>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">wenmu</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://blog.wangpengpeng.site/2020/01/08/%E5%BC%80%E5%8F%91%E5%B9%B6%E5%8F%91%E5%B8%83%E5%92%8C%E6%9B%B4%E6%96%B0%E4%B8%80%E4%B8%AAnpm%E5%8C%85/">http://blog.wangpengpeng.site/2020/01/08/%E5%BC%80%E5%8F%91%E5%B9%B6%E5%8F%91%E5%B8%83%E5%92%8C%E6%9B%B4%E6%96%B0%E4%B8%80%E4%B8%AAnpm%E5%8C%85/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noopener">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://blog.wangpengpeng.site">温木的博客</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/react/">react</a><a class="post-meta__tags" href="/tags/npm/">npm</a></div><div class="post-qr-code"><div class="post-qr-code-item"><img class="post-qr-code__img" src="https://i.loli.net/2020/02/27/hOBzIxFDJMCrmtN.jpg"><div class="post-qr-code__desc">微信打赏</div></div></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/01/08/%E5%89%8D%E7%AB%AF%E6%80%A7%E8%83%BD/"><i class="fa fa-chevron-left">  </i><span>前端性能</span></a></div><div class="next-post pull-right"><a href="/2020/01/07/hello-world/"><span>Hello World</span><i class="fa fa-chevron-right"></i></a></div></nav><div id="vcomment"></div><script src="https://cdn1.lncld.net/static/js/3.0.4/av-min.js"></script><script src="https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js"></script><script>var notify = 'false' == true ? true : false;
var verify = 'false' == true ? true : false;
var GUEST_INFO = ['nick','mail','link'];
var guest_info = 'nick,mail,link'.split(',').filter(function(item){
  return GUEST_INFO.indexOf(item) > -1
});
guest_info = guest_info.length == 0 ? GUEST_INFO :guest_info;
window.valine = new Valine({
  el:'#vcomment',
  notify:notify,
  verify:verify,
  appId:'U1QqpeQNuBnRsfygHk1GQ30q-gzGzoHsz',
  appKey:'dunDQzEseDzgsKgw4qLQcog1',
  placeholder:'支持markdown！',
  avatar:'wavatar',
  guest_info:guest_info,
  pageSize:'20',
  lang: 'zh-cn'
})</script></div></div><footer><div class="layout" id="footer"><div class="copyright">&copy;2019 - 2020 By wenmu</div><div class="framework-info"><span>驱动 - </span><a href="http://hexo.io" target="_blank" rel="noopener"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a href="https://github.com/Molunerfinn/hexo-theme-melody" target="_blank" rel="noopener"><span>Melody</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv">该文访问量<span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.6.1"></script><script src="/js/fancybox.js?version=1.6.1"></script><script src="/js/sidebar.js?version=1.6.1"></script><script src="/js/copy.js?version=1.6.1"></script><script src="/js/fireworks.js?version=1.6.1"></script><script src="/js/transition.js?version=1.6.1"></script><script src="/js/scroll.js?version=1.6.1"></script><script src="/js/head.js?version=1.6.1"></script><script>if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
}</script></body></html>